package net.zoneland.zrdp.biz.controller;

import java.net.URI;
import java.util.*;

import net.zoneland.zrdp.biz.domain.TestUserEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.zoneland.zrdp.common.annotation.Anonymous;
import net.zoneland.zrdp.common.annotation.Log;
import net.zoneland.zrdp.common.core.redis.RedisService;
import net.zoneland.zrdp.common.enums.BusinessType;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.zoneland.zrdp.common.core.controller.BaseController;
import net.zoneland.zrdp.common.core.domain.R;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;


/**
 * swagger 用户测试方法
 *
 * @author zonevue
 */
@Api("用户信息管理")
@RestController
@RequestMapping("/test/user")
public class TestController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private RedisService redisService;
    private static final Map<Integer, TestUserEntity> users = Collections.synchronizedMap(new LinkedHashMap<>());
    static {
        users.put(1, new TestUserEntity(1, "admin", "admin123", "15888888888"));
        users.put(2, new TestUserEntity(2, "zoneVue", "admin123", "15666666666"));
    }

    @ApiOperation("测试RestTemplate记录日志功能")
    @RequestMapping("test1")
    @Anonymous
    public String test1() {
        LOGGER.info("test1 getSequence:{}", redisService.getNextSequenceStr());
        final String url = "http://127.0.0.1:8080/version";
        final URI uri = UriComponentsBuilder.fromHttpUrl(url)
            .queryParam("param1", "value1")
            .queryParam("param2", "value2中文")
            .build()
            .toUri();
        final String result = restTemplate.getForObject(uri, String.class);
        LOGGER.info("result:{}", result);

        final String result1 = restTemplate.getForObject("http://127.0.0.1:8080/getAppBaseInfo", String.class);
        LOGGER.info("result1:{}", result1);

        final TestUserEntity userEntity = new TestUserEntity(3, "admin测试", "admin123", "15888888888");
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        final HttpEntity<TestUserEntity> httpEntity = new HttpEntity<>(userEntity,headers);
        final String result2 = restTemplate.postForObject("http://127.0.0.1:8080/user/add", httpEntity, String.class);
        LOGGER.info("result2:{}", result2);

        return "ok";
    }
    @ApiOperation("获取用户列表")
    @GetMapping("/list")
    @Log(title = "用户信息查看", businessType = BusinessType.OTHER)
    @Anonymous
    public R<List<TestUserEntity>> userList() {
        final List<TestUserEntity> userList = new ArrayList<>(users.values());
        return R.ok(userList);
    }

    @ApiOperation("获取用户详细")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
    @GetMapping("/{userId}")
    public R<TestUserEntity> getUser(final @PathVariable Integer userId) {
        if (!users.isEmpty() && users.containsKey(userId)) {
            return R.ok(users.get(userId));
        } else {
            return R.fail("用户不存在");
        }
    }

    @ApiOperation("新增用户")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", dataTypeClass = Integer.class),
        @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String", dataTypeClass = String.class),
        @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String", dataTypeClass = String.class),
        @ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("/save")
    public R<String> save(final TestUserEntity user) {
        if (Objects.isNull(user) || Objects.isNull(user.getUserId())) {
            return R.fail("用户ID不能为空");
        }
        users.put(user.getUserId(), user);
        return R.ok();
    }

    @ApiOperation("更新用户")
    @PostMapping("/update")
    @Anonymous
    public R<String> update(final @RequestBody TestUserEntity user) {
        if (Objects.isNull(user) || Objects.isNull(user.getUserId())) {
            return R.fail("用户ID不能为空");
        }
        if (users.isEmpty() || !users.containsKey(user.getUserId())) {
            return R.fail("用户不存在");
        }
        users.remove(user.getUserId());
        users.put(user.getUserId(), user);
        return R.ok();
    }

    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
    @DeleteMapping("/{userId}")
    public R<String> delete(final @PathVariable Integer userId) {
        if (!users.isEmpty() && users.containsKey(userId)) {
            users.remove(userId);
            return R.ok();
        } else {
            return R.fail("用户不存在");
        }
    }
}

